//
//  Fraction.m
//  oc_分数类的创建
//
//  Created by blue on 16/4/12.
//  Copyright (c) 2016年 csx. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
-(id)initWithNumerator:(NSInteger)
numerator
        andDenominator:(NSInteger)
denominator{
    self = [super init];
    if(self){
        _denominator = denominator;
        _numerator = numerator;
    }
    return self;

}

//getter方法
-(NSInteger)numerator{
    return _numerator;
}
-(NSInteger)denominator{
    return _denominator;
}

//输出分数的方法
-(void)printFraction{
    NSLog(@"%ld/%ld",_numerator,
          _denominator);
}
//两个分数对象相加,结果存入self对象
//表明结果存入调用此方法的对象中
-(void)addFraction:(Fraction *)f1{
    _numerator = _numerator*f1.denominator
    +_denominator*f1.numerator;
    _denominator = _denominator*f1.denominator;
}

/*定义方法：求两个分数对象的和，
要求：不修改源分数对象的值
 将结果直接输出
*/
+(void)addAFraction:(Fraction *)f1
       andBFraction:(Fraction *)f2{
  //定义临时变量存结果分子x1，分母x2
    /*用.获取了f1,f2对象的_denominator,_numerator
     一定要有getter方法
     */
     
    NSInteger x1,x2;
    x1 =f1.numerator*f2.denominator
    +f1.denominator*f2.numerator;
    x2 = f1.denominator*f2.denominator;
    NSLog(@"%ld/%ld",x1,x2);
}


+(Fraction *)addFraction:(Fraction *)f1 andFraction:(Fraction *)f2{
  /*将f1,f2对象相加，结果存入f1对象
    f1对象的_denominator,_numerator的值
   会改变，一定要有setter方法
   */
   f1.numerator= f1.numerator*f2.denominator
    +f1.denominator*f2.numerator;
   f2.denominator=f1.denominator*
    f2.denominator;
    return f1;
}

//分数相减
+(void)subAFraction:(Fraction *)f1
       andBFraction:(Fraction *)f2{
    f1.numerator = f1.numerator*f2.denominator-f2.numerator*f1.denominator;
    f1.denominator=f1.denominator*f2.denominator;
    NSLog(@"%ld/%ld",f1.numerator,f1.denominator);
}

+(Fraction *)subFraction:(Fraction *)f1
             andFraction:(Fraction *)f2{
    f1.numerator = f1.numerator*f2.denominator-f1.denominator*f2.numerator;
    f1.denominator = f1.denominator*f2.denominator;
    return f1;
}

@end
